# config/unicorn.rb
# Set environment to development unless something else is specified
env = ENV["RAILS_ENV"] || "production"
is_product = true
worker_processes (is_product ? 4 : 2)
app_name = "guike"
listen "/tmp/#{app_name}.socket", :backlog => 64

# Preload our app for more speed
preload_app true

# nuke workers after 30 seconds instead of 60 seconds (the default)
timeout 60
# Production specific settings
pid "/var/www/apps/#{app_name}/current/tmp/pids/guike_unicorn.pid"
if env == "production"
  working_directory "/var/www/apps/#{app_name}/current"
  shared_path = "/var/www/apps/#{app_name}/shared"
  stderr_path "#{shared_path}/log/unicorn.stderr.log"
  stdout_path "#{shared_path}/log/unicorn.stdout.log"
  old_pid = "/var/www/apps/#{app_name}/current/tmp/pids/unicorn.pid.oldbin"
end  


before_fork do |server, worker|
  # the following is highly recomended for Rails + "preload_app true"
  # as there's no need for the master process to hold a connection
  if defined?(ActiveRecord::Base)
    ActiveRecord::Base.connection.disconnect!
  end

  #Before forking, kill the master process that belongs to the .oldbin PID.
  #This enables 0 downtime deploys.

  if File.exists?(old_pid) && server.pid != old_pid
    begin
      Process.kill("QUIT", File.read(old_pid).to_i)
    rescue Errno::ENOENT, Errno::ESRCH
      # someone else did our job for us
    end
  end
end

after_fork do |server, worker|
  # the following is *required* for Rails + "preload_app true",
  if defined?(ActiveRecord::Base)
    ActiveRecord::Base.establish_connection
end


end